﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using ETA.Entity;
using ETA.IRepository;
using ETA.IService;

namespace ETA.Service
{
    public class QuestionService : IQuestionService
    {
           #region 属性

        private IQuestionRepository _IQuestionRepository { set; get; }


        #endregion

        #region 构造函数

        public QuestionService(IQuestionRepository IQuestionRepository)
        {
            _IQuestionRepository = IQuestionRepository;
        }

        #endregion


        #region IQuestionService 成员

        public QueryQuestionResponse QueryQuestionList(QueryQuestionRequest req, out int count)
        {
            count = 0;
            var response = new QueryQuestionResponse();
            try
            {
                var express = QueryQuestionExpress(req);
                var list = _IQuestionRepository.GetAllList(express);
                if (!string.IsNullOrEmpty(req.KeyWord)&&list.Any())
                {
                    list = list.FindAll(l => l.QuestionContent.Contains(req.KeyWord) || (l.AnswerContent!=null&&l.AnswerContent.Contains(req.KeyWord)));
                }

                if (req.Sdate != null)
                {
                    list = list.FindAll(l => l.CreateDate >= req.Sdate);
                }

                if (req.Edate != null)
                {
                    list = list.FindAll(l => l.CreateDate <= req.Edate);
                }

                if (req.NoAnswerFlag)
                {
                    list = list.FindAll(l => string.IsNullOrEmpty(l.AnswerContent));
                }
                count = list.Count;
                list = list.OrderByDescending(l => l.ModifyDate).ToList();
                response.QuestionList = list.Skip((req.Page - 1) * req.PageSize).Take(req.PageSize).ToList();
                response.Result.IsSuccess = true;

            }
            catch (Exception e)
            {
                response.Result.ErrMessage = e.Message;
                response.Result.IsSuccess = false;
            }
            return response;
        }

        private Expression<Func<Question, bool>> QueryQuestionExpress(QueryQuestionRequest req)
        {
            Expression<Func<Question, bool>> express = help => help.IsDelete == false;
            if (req.IsWeb)
            {
                express = help => help.IsDelete == false && help.AnswerContent != null && help.AnswerContent!="";
            }
            if (req.Question != null&&req.Question.QuestionId!=null)
            {
                express = question => question.QuestionId == req.Question.QuestionId;
            }
            return express;
        }

        public CreateQuestionResponse CreateQuestion(CreateQuestionRequest req)
        {
            var res = new CreateQuestionResponse();
            try
            {
                if (req != null)
                {
                    var qId = _IQuestionRepository.GenerateNo("", 3);
                    qId = String.Format("{0:d8}",Int64.Parse(qId));
                    req.Question.QuestionId = qId;
                    req.Question.ModifyDate = DateTime.Now;
                    res.Question = _IQuestionRepository.Insert(req.Question);
                    res.Result.IsSuccess = true;
                }
            }
            catch (Exception e)
            {
                res.Result.IsSuccess = false;
                res.Result.ErrMessage = e.Message;
            }
            return res;
        }

        public ModifyQuestionResponse ModifyQuestion(ModifyQuestionRequest req)
        {
            var res = new ModifyQuestionResponse();
            try
            {
                if (req != null)
                {
                    req.Question.ModifyDate = DateTime.Now;
                    _IQuestionRepository.Update(req.Question);
                    res.Result.IsSuccess = true;
                }
            }
            catch (Exception e)
            {
                res.Result.IsSuccess = false;
                res.Result.ErrMessage = e.Message;
            }
            return res;
        }

        #endregion
    }
}
